package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVerification;
import com.yanqu.road.entity.player.UserVerificationItem;
import com.yanqu.road.entity.player.UserWechatBind;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserWechatBindDaoImpl extends TempDataObjectDao<UserWechatBind> {
    public static String getUserWechatBindSql = "select * from `t_u_mini_bind` where `userId`=?";
    public UserWechatBind getUserWechatBind(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getUserWechatBindSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateUserWechatBindSql = "update `t_u_mini_bind` set lastGetBindTime=?,bindTimeList=?,bindCode=?,isBind=? where userId=?";
    @Override
    protected boolean updateData(UserWechatBind userWechatBind) {
        userWechatBind.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userWechatBind.getLastGetBindTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWechatBind.getBindTimeList())));
        para.put(i++, new DbParameter(Types.VARCHAR, userWechatBind.getBindCode()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userWechatBind.isBind()));
        para.put(i++, new DbParameter(Types.BIGINT, userWechatBind.getUserId()));
        if (getDb().executeNoneQuery(updateUserWechatBindSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertUserWechatBindSql = "insert into `t_u_mini_bind`(userId,lastGetBindTime,bindTimeList,bindCode,isBind) values (?,?,?,?,?)";
    @Override
    protected boolean addData(UserWechatBind userWechatBind) {
        userWechatBind.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, userWechatBind.getUserId()));
        para.put(i++, new DbParameter(Types.BIGINT, userWechatBind.getLastGetBindTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userWechatBind.getBindTimeList())));
        para.put(i++, new DbParameter(Types.VARCHAR, userWechatBind.getBindCode()));
        para.put(i++, new DbParameter(Types.BOOLEAN, userWechatBind.isBind()));
        if (getDb().executeNoneQuery(insertUserWechatBindSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected UserWechatBind parseData(ResultSet rs) throws SQLException {
        UserWechatBind userWechatBind = new UserWechatBind();
        userWechatBind.setUserId(rs.getLong("userId"));
        userWechatBind.setLastGetBindTime(rs.getLong("lastGetBindTime"));
        userWechatBind.setBindTimeList(JSON.parseObject(rs.getString("bindTimeList"), new TypeReference<java.util.List<java.lang.Long>>() {}));
        userWechatBind.setBindCode(rs.getString("bindCode"));
        userWechatBind.setBind(rs.getBoolean("isBind"));

        return userWechatBind;
    }
}
